package com.scau.action.business;

import java.util.List;

import org.springframework.stereotype.Component;

import com.scau.exception.BusinessException;
import com.scau.model.business.SupplierMaterial;
import com.scau.service.BaseService;

@Component
public class SupplierMaterialService extends BaseService<SupplierMaterial> {

	public Long save(SupplierMaterial entity) throws BusinessException {
		if (null != entity && null != entity.getSupplier_id()
				&& null != entity.getMaterial_id()
				&& 0 != entity.getSupplier_id() && 0 != entity.getMaterial_id()) {
			List<SupplierMaterial> l = list(
					new SupplierMaterial(),
					null,
					null,
					new String[] { "supplier_id", "material_id" },
					new Object[] { entity.getSupplier_id(),
							entity.getMaterial_id() });
			if (l.size() > 0) {
				return l.get(0).getId();
			} else {
				add(entity);
				return get(entity).getId();
			}
		} else {
			throw new BusinessException("id号不能为空");
		}
	}

	@Override
	public SupplierMaterial get(SupplierMaterial entity) {
		if (null != entity && null != entity.getId() && 0 != entity.getId()) {
			return super.get(entity, entity.getId());
		} else if (null != entity) {
			return super.get(entity);
		} else {
			return null;
		}
	}

}
